﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using EntityBinder;
using EntityBinder.Interface;
using System.Collections.Generic;
using UnitTest.Model;

namespace UnitTest
{
	[TestClass]
	public class EntityRulesFactoryTest
	{
		[TestMethod]
		public void TestSimpleRule()
		{
			IEntityElement ruleFactory = EntityRulesFactory.GetRules(typeof(SimpleContainer));
			IEntityElement rightRules = SimpleContainer.GetRules();

			Assert.IsTrue(RuleEquals(ruleFactory, rightRules), "EntityRulesFactory.GetRules(Type) does not return the expected rules for simple container");
		}

		[TestMethod]
		public void TestObjectRule()
		{
			IEntityElement ruleFactory = EntityRulesFactory.GetRules(typeof(ObjectContainer));
			IEntityElement rightRules = ObjectContainer.GetRules();

			Assert.IsTrue(RuleEquals(ruleFactory, rightRules), "ObjectContainer.GetRules(Type) does not return the expected rules for object container");
		}

		public bool RuleEquals(IEntityElement _element, IEntityElement _ref)
		{
			if(_element.Identifier == _ref.Identifier)
			{
				if (_element.Children.Count() == _ref.Children.Count())
				{
					foreach (IEntityElement b in _ref.Children)
					{
						IEnumerable<IEntityElement> children = from e in _element.Children
															   where e.Identifier == b.Identifier
															   select e;
						if(children.Count() != 1 || !RuleEquals(children.Single(), b))
						{
							return false;
						}
					}
					return true;
				}
			}
			return false;
		}
	}
}
